CMAKE_MINIMUM_REQUIRED (VERSION 3.10)
PROJECT(CPPJIEBA)

find_package(limonp)
if(limonp_FOUND)
    get_target_property(LIMONP_INCLUDE_DIR limonp::limonp INTERFACE_INCLUDE_DIRECTORIES)
else()
    set(LIMONP_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/deps/limonp/include")
endif()
INCLUDE_DIRECTORIES("${LIMONP_INCLUDE_DIR}"
  ${PROJECT_SOURCE_DIR}/include)

if(NOT DEFINED CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

ADD_DEFINITIONS(-O3 -g)

# Define a variable to check if this is the top-level project
if(NOT DEFINED CPPJIEBA_TOP_LEVEL_PROJECT)
    if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
        set(CPPJIEBA_TOP_LEVEL_PROJECT ON)
    else()
        set(CPPJIEBA_TOP_LEVEL_PROJECT OFF)
    endif()
endif()

if(NOT TARGET cppjieba)
  add_library(cppjieba INTERFACE)
  target_include_directories(cppjieba INTERFACE
    ${PROJECT_SOURCE_DIR}/include
    ${PROJECT_SOURCE_DIR}/deps/limonp/include
  )
endif()

# 添加 x_cmp_jieba.dll 共享库目标
add_library(x_cmp_jieba SHARED src/x_cmp_jieba.cpp)
target_include_directories(x_cmp_jieba PRIVATE
  ${PROJECT_SOURCE_DIR}/include
  ${PROJECT_SOURCE_DIR}/deps/limonp/include
)
set_target_properties(x_cmp_jieba PROPERTIES
  OUTPUT_NAME "x_cmp_jieba"
  PREFIX ""
)

# 添加安装规则，用于安装 x_cmp_jieba.dll 和 dict 目录
install(TARGETS x_cmp_jieba
        RUNTIME DESTINATION bin
        COMPONENT x_cmp_jieba)

# 安装 dict 目录到 bin/dict 下
install(DIRECTORY dict/
        DESTINATION bin/dict
        COMPONENT x_cmp_jieba_dict)

# 创建一个自定义安装目标，只安装我们需要的文件
add_custom_target(install_x_cmp_jieba
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_COMPONENT=x_cmp_jieba -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
    COMMAND ${CMAKE_COMMAND} -DCMAKE_INSTALL_COMPONENT=x_cmp_jieba_dict -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_install.cmake
    COMMENT "Installing only x_cmp_jieba components"
)

if(CPPJIEBA_TOP_LEVEL_PROJECT)
  ENABLE_TESTING()

  message(STATUS "MSVC value: ${MSVC}")
  ADD_SUBDIRECTORY(test)
  ADD_TEST(NAME ./test/test.run COMMAND ./test/test.run)
  ADD_TEST(NAME ./load_test COMMAND ./load_test)
endif()
